'''
Created on 22.11.2009

@author: ulakorotneva
'''

from google.appengine.api import users
import os
from google.appengine.ext.webapp import template
from ru.simpleprojecttool.db.model import *

class Error(object):
    message = str
    level = str
    def __init__(self, message, level):
        self.message = message
        self.level = level
        
def showTemplate(requestHandler, templatePath, errors, template_values):
    template_values['currentUser'] = users.get_current_user()
    template_values['errors'] = errors
    path = os.path.join(os.path.dirname(__file__), templatePath)
    requestHandler.response.out.write(template.render(path, template_values))

def isParticipant(project):
    currentUser = users.get_current_user()
    participants = Participant.all().filter("project = ", project).filter("user = ", currentUser).fetch(1)
    if len(participants) > 0:
        return True
    else:
        return False
    
def canEditProject(project):
    currentUser = users.get_current_user()
    participants = Participant.all().filter("project = ", project).filter("user = ", currentUser).fetch(1)
    
    canGo = False
    if len(participants) > 0:
        pRoles = ParticipantRole.all().filter("participant = ", participants[0])
        
        for pRole in pRoles:
            if pRole.role.canEditProject:
                canGo = True
                break 
    return canGo

def participantCanEditProject(participant):
    canGo = False
    pRoles = ParticipantRole.all().filter("participant = ", participant)
    
    for pRole in pRoles:
        if pRole.role.canEditProject:
            canGo = True
            break 
    return canGo

def participantCanBeAssigned(participant):
    canGo = False
    pRoles = ParticipantRole.all().filter("participant = ", participant)
    
    for pRole in pRoles:
        if pRole.role.canBeAssigned:
            canGo = True
            break 
    return canGo

def canAssign(project):
    currentUser = users.get_current_user()
    participants = Participant.all().filter("project = ", project).filter("user = ", currentUser).fetch(1)
    
    canGo = False
    if len(participants) > 0:
        pRoles = ParticipantRole.all().filter("participant = ", participants[0])
        
        for pRole in pRoles:
            if pRole.role.canAssign:
                canGo = True
                break 
    return canGo

def getParticipantByUser(project):
    currentUser = users.get_current_user()
    participants = Participant.all().filter("project = ", project).filter("user = ", currentUser).fetch(1)
    return participants[0]

def canBeAssigned(project):
    currentUser = users.get_current_user()
    participants = Participant.all().filter("project = ", project).filter("user = ", currentUser).fetch(1)
    
    canGo = False
    if len(participants) > 0:
        pRoles = ParticipantRole.all().filter("participant = ", participants[0])
        
        for pRole in pRoles:
            if pRole.role.canBeAssigned:
                canGo = True
                break 
    return canGo

def checkProject(project, requestHandler):
    if canEditProject(project):
        requestHandler.execute()
    else:
        showTemplate(requestHandler, '../../../templates/error.html', [Error(message = 'You can\'t access this page', level = 'error')], {})
    
def checkCanAssign(project, requestHandler):
    if canAssign(project):
        requestHandler.execute()
    else:
        showTemplate(requestHandler, '../../../templates/error.html', [Error(message = 'You can\'t create tasks', level = 'error')], {})

def checkCanBeAssigned(project, requestHandler):
    if canBeAssigned(project):
        requestHandler.execute()
    else:
        showTemplate(requestHandler, '../../../templates/error.html', [Error(message = 'You can\'t be assigned to tasks', level = 'error')], {})

def checkParticipant(project, requestHandler):
    if isParticipant(project):
        requestHandler.execute()
    else:
        showTemplate(requestHandler, '../../../templates/error.html', [Error(message = 'You are not a participant in the current project' , level = 'error')], {})
        
''' 

(creator & canAssign) can change anything in task
(assignedTo) can change status to completed or cancelled   
'''       
def canEditTask(task):
    currentUser = users.get_current_user() 
    if task.assignedTo.user == currentUser:
        return True
    else:
        if task.creator == currentUser:
            if canAssign(task.project):
                return True
            else:
                return False
        return False

def canDeleteTask(task):
    currentUser = users.get_current_user() 
    if task.creator == currentUser and task.status == 'CREATED':
        return True
    else:
        return False

def checkCanEditTask(task, requestHandler):
    if canEditTask(task):
        requestHandler.execute()
    else:
        showTemplate(requestHandler, '../../../templates/error.html', [Error(message = 'You can not edit this task' , level = 'error')], {})
             
def checkDeleteTask(task, requestHandler):
    if canDeleteTask(task):
        requestHandler.execute()
    else:
        showTemplate(requestHandler, '../../../templates/error.html', [Error(message = 'You can not delete this task' , level = 'error')], {})
       